Page({
  data: {
    carIndex: 0,
    carInfo: [],
    image: [
      'https://img.v.mifile.cn/nr-upload-car/image_948c0dc643dd48d48aa711707155d220_1711189162526.jpg',
      'https://img.v.mifile.cn/nr-upload-car/image_e87d02a5af3d41679cc816e7b23d6475_1711194658595.jpg',
      'https://img.v.mifile.cn/nr-upload-car/image_4cbcc770a0064e89992a9eb4a2e7ac19_1711194659195.jpg',
    ],
  },

  async onLoad(options) {
    await this.loadVehicleDetail();
  },

  async loadVehicleDetail() {
    wx.showLoading({
      title: '加载中',
    });

    try {
      await wx.request({
        url: 'http://1.94.51.11:8850/carInfo',
        success: res => {
          const carData = res.data.data;
          this.toStringArr(carData);
          this.setData({
            carInfo: carData,
          });
        },
      });
    } catch (error) {
      console.error(error);
    } finally {
      wx.hideLoading();
    }
  },

  toStringArr(carData) {
    carData.forEach(car => {
      car.info = car.info.split(',');
      car.price = Number(car.price); // 转换科学计数法字符串为数字
    });
  },

  handleTap(event) {
    const index = event.currentTarget.dataset.index;
    this.setData({
      carIndex: index,
    });
  },

  goToCarSetPage(e) {
    const carId = this.data.carInfo[this.data.carIndex].id;
    const price = this.data.carInfo[this.data.carIndex].price;
    wx.navigateTo({
      url: `/pages/carSet/carSet?carId=${carId}&price=${price}`,
    });
  },
});
